///////////////////////////////////////////////////////////////////////////////
// 
// JK_HEADER
//
// This script is for dynamic behaviour in the header bar
//
///////////////////////////////////////////////////////////////////////////////

/**
 * Create global namespace 
 * @type {object}
 */
var jk_hd = {};

/**
 * Run header script for jeemang.ca header
 * @param {none} 
 */
jk_hd.header = function() {
	
	// hide tickers/clocks (all of class "HDRight") when window width < hrl
	var hrl = 755; // hrl === header right limit; from 1140 px grid; see utils/1140.css

	if ( $(window).width() < hrl ) { $(".HDRight").hide() } 

	$(window).resize(function() {
	
		var window_width = $(window).width();
	
		if (window_width < hrl) {
			$(".HDRight").hide();
		}
	
		if (window_width >= hrl) {
			$(".HDRight").show();
		}
	
	});
	
} // END jk.header()

/**
 * Run jeemang.ca clock
 * @param {none} 
 */
jk_hd.run_clock = function() {
		
	// refresh clock
	setInterval ( function() {
		
		// get date to determine if daylight savings in effect and build timezone string
		// AB: DST starts second sunday in march, ends first sunday in november	
		// getDay: 0 = sunday, 1 = monday, etc
		var d = new Date();
		var day = d.getDay();
		var date = d.getDate();
		var month = d.getMonth();
		
		var dst;
		if ( month >= 3 || month <= 9 ) { dst = true }
		else if ((month == 3 ) && (date - day >= 7)) { dst = true }
		else if ((month == 10) && (date - day <= 0)) { dst = true }
		else dst = false;
		
		var timezone_str = dst ? "MDT" : "MST";
		
		// built seconds, minutes, and hours strings
		var seconds = d.getSeconds();
		var seconds_str = ( seconds < 10 ? "0" + seconds : "" + seconds );
		
		var minutes = d.getMinutes();
		var minutes_str = ( minutes < 10 ? "0" + minutes : "" + minutes );
		
		// convert hours to mountain time
		var utc_hours = d.getUTCHours();
		var utc_offset = dst ? 6 : 7;
		var hours = utc_hours = utc_hours - utc_offset;
		if (hours < 0) { hours = 24 + hours }
		var hours_str = (hours < 10 ? "0" + hours : "" + hours );
		
		// build time string and set
		var time_str = hours_str + ":" + minutes_str + ":" + seconds_str + " " + timezone_str
		
		$("#hd_clock").html(time_str);
		
	},1000); // END refresh clock
	
	
} // END jk.run_clock()

$(document).ready(function() {
	
	// HEADER
	
	// start clock in header
	jk_hd.run_clock();
	
	// header javascript
	jk_hd.header();
	
});